package com.xintonghua.bussiness.ui.fragment.client.user;

import android.content.Intent;
import android.os.Bundle;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.base.ui.MySubscriber;
import com.xintonghua.base.utils.RefreshUtils;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.ClubCardAdapter;
import com.xintonghua.bussiness.api.HttpCent;
import com.xintonghua.bussiness.api.manager.AuthManager;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.ClubCardBean;
import com.xintonghua.bussiness.bean.Contant;
import com.xintonghua.bussiness.bean.UserInfoBean;
import com.xintonghua.bussiness.util.JsonUtil;
import com.xintonghua.bussiness.util.JumpUtils;
import com.xintonghua.bussiness.util.MyState;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * 会员卡
 * liqi 2018-3-31
 */
public class ClubCardActivity extends BaseActivity implements XRecyclerView.LoadingListener {

    @BindView(R.id.recycler_club_card)
    XRecyclerView recyclerClubCard;
    private ClubCardAdapter adapter;
    Contant contantbean;
    List<ClubCardBean.ListBean> mlist;

    UserInfoBean userInfoBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_card);
        ButterKnife.bind(this);
        onSimpleActionBar();
        setSimpleActionBar("会员卡");
        contantbean = (Contant) JumpUtils.getSerializable(ClubCardActivity.this, "data0");
        userInfoBean = (UserInfoBean) JumpUtils.getSerializable(ClubCardActivity.this, "data1");
//        bun.putSerializable("data1",(Serializable) userInfoBean);
        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        mlist = new ArrayList<>();
        RefreshUtils.initList(this, recyclerClubCard);
        recyclerClubCard.setPullRefreshEnabled(true);
        recyclerClubCard.setLoadingMoreEnabled(true);
        adapter = new ClubCardAdapter(this, mlist);
        adapter.setOnRechargeClickClistener(new ClubCardAdapter.RechargeInterface() {
            @Override
            public void rechargeClistener(ClubCardBean.ListBean bean, int position) {

                Bundle bun = new Bundle();
                bun.putSerializable("data0", contantbean);
                bun.putSerializable("data1", bean);
                bun.putSerializable("data2", (Serializable) userInfoBean);

                JumpUtils.jumptoB(ClubCardActivity.this, RechargeMemberCardActivity.class, bun);
            }
        });
        recyclerClubCard.setAdapter(adapter);
        recyclerClubCard.setLoadingListener(this);
        onRefresh();
    }

    @Override
    public void onRefresh() {
        page = 1;
        HttpCent.getInstance(this).getUserMemberCard(userInfoBean.getId() + "", "" + page, this, MyState.requestOne);
    }

    int page = 1;

    @Override
    public void onLoadMore() {
        page++;
        HttpCent.getInstance(this).getUserMemberCard(userInfoBean.getId() + "", "" + page, this, MyState.requestOne);

    }


    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        try {
            switch (order) {
                case MyState.requestOne:
                    ClubCardBean bean = JsonUtil.getEntityByJsonString(data.toString(), ClubCardBean.class);
                    List<ClubCardBean.ListBean> list = bean.getList();
                    if (page == 1) {
                        mlist.clear();
                    }
                    mlist.addAll(list);
                    adapter.notifyDataSetChanged();
                    recyclerClubCard.loadMoreComplete();
                    recyclerClubCard.refreshComplete();
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
